package cn.tedu.hospitalqd.service.impl;

import cn.tedu.hospitalqd.ex.ServiceException;
import cn.tedu.hospitalqd.mapper.DrugMapper;
import cn.tedu.hospitalqd.pojo.dto.DrugAddNewDTO;
import cn.tedu.hospitalqd.pojo.dto.DrugUpdateDTO;
import cn.tedu.hospitalqd.pojo.entity.Drug;
import cn.tedu.hospitalqd.pojo.vo.DrugListItemVO;
import cn.tedu.hospitalqd.pojo.vo.DrugStandardVO;
import cn.tedu.hospitalqd.service.DrugService;
import cn.tedu.hospitalqd.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class DrugServiceImpl implements DrugService {

    @Autowired
    private DrugMapper drugMapper;

    public DrugServiceImpl(){log.debug("创建业务类对象：DrugServiceImpl");}

    @Override
    public void addNew(DrugAddNewDTO drugAddNewDTO) {
        log.debug("开始处理【添加药品】的业务，参数：{}",drugAddNewDTO);
        String name = drugAddNewDTO.getYpName();
        int count = drugMapper.countByName(name);
        log.debug("根据名称【{}】统计数量，结果：{}",name,count);
        if(count > 0){
            //是：抛出异常throw new RuntimeException()
            String message = "添加药品失败，药品名称已经被占用!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Drug drug = new Drug();
        BeanUtils.copyProperties(drugAddNewDTO,drug);

        log.debug("即将执行插入数据，参数：{}",drug);
        int rows = drugMapper.insert(drug);
        if(rows != 1){
            String message = "添加药品失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除相册】的业务，参数：{}",id);
        DrugStandardVO drug = drugMapper.getStandardById(id);
        if(drug == null){
            String message = "删除药品失败，尝试访问的数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        log.debug("即将执行删除，参数：{}",id);
        int rows = drugMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除药品失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Long id, DrugUpdateDTO drugUpdateDTO) {
        log.debug("开始处理【修改相册详情】的业务，ID：{}，新数据：{}",id,drugUpdateDTO);
        DrugStandardVO queryResult = drugMapper.getStandardById(id);
        if(queryResult == null){
            String message = "修改药品详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        int count = drugMapper.countByNameAndNotId(id,drugUpdateDTO.getYpName());
        if(count > 0){
            String message = "修改药品详情失败，药品名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Drug drug = new Drug();
        BeanUtils.copyProperties(drugUpdateDTO,drug);
        drug.setId(id);
        log.debug("即将执行修改数据，参数：{}",drug);
        int rows = drugMapper.update(drug);
        if (rows != 1) {
            String message = "修改相册详情失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public DrugStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询相册详情】的业务，参数：{}", id);
        DrugStandardVO queryResult = drugMapper.getStandardById(id);
        if(queryResult == null){
            String message = "获取药品详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return queryResult;
    }

    @Override
    public List<DrugListItemVO> list() {
        log.debug("开始处理【查询相册列表】的业务，无参数");
        List<DrugListItemVO> list = drugMapper.list();
        return list;
    }
}
